package com.springboot.middleware.middleware_lock.server.controller;

import com.springboot.middleware.middleware_lock.api.enums.StatusCode;
import com.springboot.middleware.middleware_lock.api.response.BaseResponse;
import com.springboot.middleware.middleware_lock.server.dto.UserAccountDto;
import com.springboot.middleware.middleware_lock.server.service.DataBaseLockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基于数据库的乐观锁和悲观锁
 */
@RestController
public class DataBaseLockController {
    private static final Logger log = LoggerFactory.getLogger(DataBaseLockController.class);
    private static final String prefix = "db";

    @Autowired
    DataBaseLockService dataBaseLockService;

    /**
     * 用户账户余额提现申请-不加锁
     * @param dto UserAccountDto
     * @return BaseResponse
     */
    @RequestMapping(value = prefix+"/money/take", method = RequestMethod.GET)
    public BaseResponse takeMoney(UserAccountDto dto) {
        //合法性检查
        if (dto.getAmount() == null || dto.getUserId() == null) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            dataBaseLockService.takeMoney(dto);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }

    /**
     * 用户账户余额提现申请-加乐观锁
     * @param dto UserAccountDto
     * @return BaseResponse
     */
    @RequestMapping(value = prefix+"/money/optimistic", method = RequestMethod.GET)
    public BaseResponse takeMoneyWithOptimisticLock(UserAccountDto dto) {
        //合法性检查
        if (dto.getAmount() == null || dto.getUserId() == null) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            dataBaseLockService.takeMoneyWithLock(dto);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }

    /**
     * 用户账户余额提现申请-加悲观锁
     * @param dto UserAccountDto
     * @return BaseResponse
     */
    @RequestMapping(value = prefix+"/money/negative", method = RequestMethod.GET)
    public BaseResponse takeMoneyWithNegativeLock(UserAccountDto dto) {
        //合法性检查
        if (dto.getAmount() == null || dto.getUserId() == null) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            dataBaseLockService.takeMoneyWithNegativeLock(dto);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }
}
